/**
 * 统计某个游戏/所有游戏的输赢信息
 */
cmd.prototype.bet_sum_query = function(type, start, end, owner, cb) {
    var self = this;
    var data = {
        pageNum: 1,
        pageSize: 5000,
    };
    if (type) data.type = type;
    if (owner) data.owner = owner;
    if (start) {
        if (start <= end) {
            data.start = start;
            data.end = end;
        }
        else {
            data.start = end;
            data.end = start;
        }
    }
    self.post("bet/sum/dquery", data, cb);
}
cmd.prototype.bet_sum_mquery = function(type, start, end, owner, cb) {
    var self = this;
    var data = {};
    if (type) data.type = type;
    if (owner) data.owner = owner;
    if (start) {
        if (start <= end) {
            data.start = start;
            data.end = end;
        }
        else {
            data.start = end;
            data.end = start;
        }
    }
    self.post("bet/sum/mquery", data, cb);
}

cmd.prototype.account_pending = function(id, owner, pending, cb) {
    var self = this;
    var data = {
        user: id,
        owner: owner || '',
        pending: pending,
    };
    self.post("account/pending", data, cb);
}

/**
 * 账户总余额
 */
cmd.prototype.account_balance = function(cb) {
    var self = this;
    self.post("account/balance", {}, cb);
}

/**
 * 会员/代理报表
 */
cmd.prototype.stat_user_query = function(type, referee, uid, start, end, 
    profitStart, profitEnd, isDaili, pageSize, pageNum, cb) {
    var self = this;
    var data = {
        pageSize: pageSize || 100,
        pageNum: pageNum || 1,
        refereeStart: isDaili ? 1 : 0,
    };
    if (type) data.type = type;
    if (referee) data.referee = referee;
    if (uid) data.uid = uid;
    if (start) data.start = start;
    if (end) data.end = end;
    if (profitStart !== undefined) data.profitStart = profitStart * 100;
    if (profitEnd !== undefined) data.profitEnd = profitEnd * 100;
    self.post("stat/user/query", data, cb);
}
cmd.prototype.stat_user_squery = function(data, cb) {
    var self = this;
    /*
    var data = {
        pageSize: pageSize || 100,
        pageNum: pageNum || 1,
        refereeStart: isDaili ? 1 : 0,
    };
    if (type) data.type = type;
    if (referee) data.referee = referee;
    if (uid) data.uid = uid;
    if (start) data.start = start;
    if (end) data.end = end;
    if (profitStart !== undefined) data.profitStart = profitStart * 100;
    if (profitEnd !== undefined) data.profitEnd = profitEnd * 100; */
    self.post("stat/user/squery", data, cb);
}
cmd.prototype.stat_user_sreferee = function(data, cb) {
    var self = this;
    self.post("stat/user/sreferee", data, cb);
}

/**
 * 重新统计日报表
 */
cmd.prototype.stat_sum_restat = function(date, cb) {
    var self = this;
    self.post("stat/sum/restat", {
        date: date,
    }, cb);
} 

/**
 * 撤单
 */
cmd.prototype.bet_cancel = function(type, issue, cb) {
    var self = this;
    self.post("bet/cancel", {
        type: type,
        issue: issue,
    }, cb);
}

/**
 * 重新开奖
 */
cmd.prototype.bet_reopen = function(type, issue, cb) {
    var self = this;
    self.bet_cancel(type, issue, function() {
        self.post("bet/reopen", {
            type: type,
            issue: issue,
        }, cb);
    });
}

/**
 * 踢人
 */
cmd.prototype.offline = function(id, cb) {
    var self = this;
    self.post("user/online/sign-out", {
        user: id,
    }, cb);
}

/**
 * 修改用户状态
 */
cmd.prototype.user_state = function(id, state, cb) {
    var self = this;
    self.post("user/state", {
        id: id,
        state: state,
    }, cb);
}

/**
 * 检索IP黑名单
 */
cmd.prototype.blacklist_query = function(type, pageNum, pageSize, cb) {
    var self = this;
    self.post("blacklist/query", {
        type: type,
        pageNum: pageNum || 1,
        pageSize : pageSize || 100,
    }, cb);
}

/**
 * 保存IP黑名单
 */
cmd.prototype.blacklist_save = function(type, ip, memo, cb) {
    var self = this;
    self.post("blacklist/save", {
        type: type,
        ip: ip,
        memo: memo || ''
    }, cb);
}

/**
 * 删除IP黑名单
 */
cmd.prototype.blacklist_delete = function(id, cb) {
    var self = this;
    self.post("blacklist/delete", {
        id: id,
    }, cb);
}

/**
 * 设置分红比例
 */
cmd.prototype.bonus_rate_query = function(cb) {
    var self = this;
    self.post("stat/bonus-rate/query", {}, cb);
}
cmd.prototype.bonus_rate_save = function(data, cb) {
    var self = this;
    self.post("stat/bonus-rate/save", data, cb);
}
cmd.prototype.bonus_rate_delete = function(id, cb) {
    var self = this;
    self.post("stat/bonus-rate/delete", {
        id: id,
    }, cb);
}

/**
 * 获取在线用户
 */
cmd.prototype.user_online_query = function(data, cb) {
    var self = this;
    self.post("user/online/query", data, function(r) {
        cb(r);
    });
}

// 冻结/解冻用户
cmd.prototype.user_state = function(id, state, cb) {
    var self = this;
    self.post("user/state", {
        id: id,
        state: state || 0,
    }, cb);
}

// 关注用户
cmd.prototype.user_tail = function(id, tail, cb) {
    var self = this;
    self.post("user/tail", {
        id: id,
        tail: tail || 0,
    }, cb);
}

/**
 * 获取未处理账变
 */
cmd.prototype.account_log_state0 = function(cb) {
    var self = this;
    self.post("account/log/state0", {}, cb);
}

/**
 * 修改代理
 */
cmd.prototype.user_referee = function(id, referee, cb) {
    var self = this;
    self.post("user/referee", {
        id: id,
        referee: referee,
    }, cb);
}

/**
 * 代理统计
 */
cmd.prototype.stat_referee_query = function(data, cb) {
    var self = this;
    self.post("stat/referee/query", data, cb);
}

/**
 * 代理结算
 */
cmd.prototype.stat_referee_accumulate = function(id, memo, cb) {
    var self = this;
    var data = {
        id: id,
        memo: memo || '',
    };
    self.post("stat/referee/accumulate", data, cb);
}
cmd.prototype.stat_referee_settle = function(id, memo, cb) {
    var self = this;
    var data = {
        id: id,
        memo: memo || '',
    };
    self.post("stat/referee/settle", data, cb);
}
cmd.prototype.stat_referee_clear = function(id, memo, cb) {
    var self = this;
    var data = {
        id: id,
        memo: memo || '',
    };
    self.post("stat/referee/clear", data, cb);
}
cmd.prototype.stat_referee_pass = function(id, cb) {
    var self = this;
    self.post("stat/referee/pass", {
        id: id
    }, cb);
}
cmd.prototype.stat_referee_reject = function(id, memo, cb) {
    var self = this;
    self.post("stat/referee/reject", {
        id: id,
        memo: memo || "",
    }, cb);
}

// DEBUG指令
cmd.prototype.stat_referee_zero = function(id, memo, cb) {
    var self = this;
    var data = {
        id: id,
        memo: memo || '',
    };
    self.post("stat/referee/zero", data, cb);
}

/**
 * 权限操作
 */
cmd.prototype.crosier_query = function(cb) {
    var self = this;
    self.post("user/crosier/query", {}, cb);
}
cmd.prototype.crosier_save = function(data, cb) {
    var self = this;
    self.post("user/crosier/saves", data, cb);
}
cmd.prototype.crosier_delete = function(uri, cb) {
    var self = this;
    self.post("user/crosier/deletes", { uris: uri }, cb);
}

/**
 * 修改用户等级
 */
cmd.prototype.user_grade = function(id, grade, cb) {
    var self = this;
    self.post("user/grade", {
        id: id,
        grade: grade,
    }, cb);
}

/**
 * 重置密码
 */
cmd.prototype.reset_password = function(id, password, cb) {
    var self = this;
    self.post("user/reset-pass", {
        id: id,
        password: password,
    }, cb);
}

/**
 * 操作日志查看
 */
cmd.prototype.journal_query = function(data, cb) {
    var self = this;
    /*
    var data = {
        pageSize: pageSize || 200,
        pageNum: page || 1,
    };
    if (uid) data.uid = uid;
    if (uri) data.uri = uri;
    if (start) data.start = start;
    if (end) data.end = end; */
    self.post("journal/query", data, cb);
}

/**
 * 获取用户列表
 */
cmd.prototype.get_user_list = function(data, pageNum, pageSize, cb) {
    var self = this;
    data.pageSize = pageSize || 100;
    data.pageNum = pageNum || 1;
    self.post("user/query", data, cb);
}

/**
 * 踢下线
 */
cmd.prototype.force_signout = function(id, cb) {
    var self = this;
    self.post("user/online/sign-out", {
        user: id,
    }, cb);
}

/**
 * 登录日志查看
 */
cmd.prototype.user_log_query = function(data, cb) {
    var self = this;
    self.post("user/log/query", data, cb);
}
cmd.prototype.user_failure_query = function(data, cb) {
    var self = this;
    self.post("user/failure/query", data, cb);
}

/**
 * 添加账号
 */
cmd.prototype.add_account = function(uid, password, name, grade, cb) {
    var self = this;
    self.post("user/create", {
        mobile: uid,
        password: password,
        grade: grade,
        name: name,
    }, cb);
}

/**
 * 添加代理
 */
cmd.prototype.add_referee = function(uid, password, name, type, cb) {
    var self = this;
    self.post("user/create", {
        mobile: uid,
        password: password,
        grade: 49,
        name: name,
    }, function(r) {
        console.log("成功", r);
        CMD.user_referee_type(r.id, type, function() {
            if (cb) cb(r);
        });
    }, function(r) {
        if (cb) cb(r)
    });
}

/**
 * 设置赔率
 */
cmd.prototype.lottery_rate_save = function(type, data, cb) {
    var self = this;
    data.type = type;
    self.post("lottery/rate/saves", data, cb);
}
cmd.prototype.lottery_rate_usaves = function(id, type, data, cb) {
    var self = this;
    data.owner = id;
    data.type = type;
    self.post("lottery/rate/usaves", data, cb);
}

// 充值赔率
cmd.prototype.lottery_rate_reset = function(type, owner, cb) {
    var self = this;
    var data = {};
    data.owner = owner;
    if (type)
        data.type = type;
    self.post("lottery/rate/reset", data, cb);
}

/**
 * 设置代理的赔率
 */
cmd.prototype.lottery_rate_rsaves = function(type, data, cb) {
    var self = this;
    data.type = type;
    self.post("lottery/rate/rsaves", data, cb);
}

/**
 * 设置返水
 */
cmd.prototype.lottery_back_save = function(type, data, cb) {
    var self = this;
    data.type = type;
    data.owner
    self.post("lottery/rate/backs", data, cb);
}
cmd.prototype.lottery_back_usaves = function(id, type, data, cb) {
    var self = this;
    data.type = type;
    data.owner = id;
    self.post("lottery/rate/ubacks", data, cb);
}

/**
 * 设置代理返水
 */
cmd.prototype.lottery_rate_rbacks = function(type, data, cb) {
    var self = this;
    data.type = type;
    self.post("lottery/rate/rbacks", data, cb);
}

/**
 * 设置单注的限额
 */
cmd.prototype.lottery_rate_ranges = function(type, data, cb) {
    var self = this;
    data.type = type;
    self.post("lottery/rate/ranges", data, cb);
}
cmd.prototype.lottery_rate_uranges = function(id, type, data, cb) {
    var self = this;
    data.type = type;
    data.owner = id;
    self.post("lottery/rate/uranges", data, cb);
}

/**
 * 修改账户的dw字段
 */
cmd.prototype.modify_account_dw = function(id, amount, cb) {
    var self = this;
    self.post("account/dw", {
        user: id,
        owner: "",
        amount: amount,
    }, cb);
}

/**
 * 代理结算配置
 */
cmd.prototype.user_referee_type_rate = function(data, cb) {
    var self = this;
    data.type = 1;
    self.post("user/referee-type-rate", data);

    // 2种代理类型认为永远一致
    data.type = 2;
    self.post("user/referee-type-rate", data, cb);
}

/**
 * 用户层级操作
 */
cmd.prototype.user_layer_query = function(cb) {
    var self = this;
    self.post("user/layer/query", {}, cb);
}
cmd.prototype.user_layer_save = function(level, data, cb) {
    var self = this;
    data.level = level;
    self.post("user/layer/save", data, cb);
}
cmd.prototype.user_layer_delete = function(id, cb) {
    var self = this;
    self.post("user/layer/delete", {
        id: id
    }, cb);
}
cmd.prototype.user_layer_reset = function(cb) {
    var self = this;
    self.post("user/layer/reset", {}, cb);
}
cmd.prototype.user_layer = function(id, layer, lock, cb) {
    var self = this;
    var data = {
        id: id,
        lock: lock ? 1 : 0,
    };
    if (layer !== undefined) data.layer = layer;
    self.post("user/layer", data, cb);
}

/**
 * 设置代理的模式
 */
cmd.prototype.user_referee_type = function(id, type, cb) {
    var self = this;
    var data = {
        id: id,
        type: type,
    };
    self.post("user/referee-type-rate", data, cb);
}

/**
 * 微信登录信息
 */
cmd.prototype.weixin_save = function(data, cb) {
    var self = this;
    self.post("weixin/save", data, cb);
}
cmd.prototype.weixin_query = function(cb) {
    var self = this;
    self.post("weixin/query", {}, function(r) {
        if (!Util.isEmpty(r)) cb(r[0]);
        else cb({});
    });
}

/**
 * 开奖AI
 */
cmd.prototype.lottery_ai_query = function(type, cb) {
    var self = this;
    self.post("lottery/ai/query", {
        type: type,
        pageSize: 10,
        pageNum: 1,
    }, cb);
}
cmd.prototype.lottery_ai_save = function(data, cb) {
    var self = this;
    self.post("lottery/ai/save", data, cb);
}
cmd.prototype.lottery_ai_state = function(id, state, cb) {
    var self = this;
    self.post("lottery/ai/state", {
        id: id,
        state: state,
    }, cb);
}

/**
 * 设置开奖结果（中盈）
 */
cmd.prototype.set_zy_lottery_result = function(type, issue, results, cb) {
    var self = this;
    var data = {
        type: type,
        issue: issue,
        red: results.join(","),
        mem: false,
    };
    self.post("lottery/zy/save", data, cb);
}
cmd.prototype.set_lottery_result = function(type, issue, results, cb) {
    var self = this;
    var data = {
        type: type,
        issue: issue,
        red: results.join(","),
    };
    self.post("lottery/save", data, cb);
}

/**
 * 检索设置的开奖结果（中盈） 
 */
cmd.prototype.query_zy_lottery_result = function(cb) {
    var self = this;
    self.post("lottery/zy/query", {
        pageSize: 50,
        pageNum: 1
    }, cb);
}

/**
 * 设置开奖时间和结果（立刻起效）
 */
cmd.prototype.set_lottery_save = function(type, issue, results, time, cb) {
    var self = this;
    var data = {
        type: type,
        issue: issue,
        red: results.join(","),
        mem: false,
    };
    self.post("lottery/save", data, cb);
}

/**
 * 为群绑定彩种
 */
cmd.prototype.bind_group_lottery = function(groupId, type, cb) {
    var self = this;
    self.post("group/lottery", {
        id: groupId,
        lottery: type,
    }, cb);
}

/**
 * dw字段的日志检索
 */
cmd.prototype.query_dw_log = function(data, cb) {
    var self = this;
    self.post("account/log/dwquery", data, cb);
}
cmd.prototype.pass_dw_log = function(id, cb) {
    var self = this;
    self.post("account/log/dwpass", {
        ids: id,
    }, cb);
}
cmd.prototype.reject_dw_log = function(id, cb) {
    var self = this;
    self.post("account/log/dwreject", {
        ids: id,
    }, cb);
}

/**
 * ip绑定
 */
cmd.prototype.user_ips = function(id, ips, cb) {
    var self = this;
    self.post("user/ips", {
        id: id,
        ips: ips,
    }, cb);
}

/**
 * 第三方充值
 */
cmd.prototype.payment_query = function(data, cb) {
    var self = this;
    self.post("payment/query", data, cb)
}
cmd.prototype.payment_notice = function(id, cb) {
    var self = this;
    self.post("payment/notice", {
        id: id
    }, cb)
}
cmd.prototype.payment_success = function(id, cb) {
    var self = this;
    self.post("payment/success", {
        id: id
    }, cb);
}

cmd.prototype.pay_query = function(cb) {
    var self = this;
    self.post("pay/query", {
        pageNum: 1,
        pageSize: 200,
    }, cb)
}
cmd.prototype.pay_save = function(data, cb) {
    var self = this;
    self.post("pay/save", data, cb)
}
cmd.prototype.pay_delete = function(id, cb) {
    var self = this;
    self.post("pay/delete", { id: id }, cb)
}
